﻿using System;

namespace OneWorks.Web.Common.BaiDu.Place
{
    public class BaiduPlaceSearchParams : BaiduPlaceParams
    {
        /// <summary>
        /// 检索关键字 中关村、ATM、百度大厦
        /// </summary>
        public string Query = "";

        /// <summary>
        /// 检索区域，如果取值为“全国”或某省份，则返回指定区域的POI。 北京、131、全国
        /// </summary>
        public string Region = "北京";

        /// <summary>
        /// 检索矩形区域 38.76623,116.43213,39.54321,116.46773 lat,lng(左下角坐标),lat,lng(右上角坐标)
        /// </summary>
        public string Bounds = "";

        /// <summary>
        /// 周边检索中心点，不支持多个点 38.76623,116.43213 lat纬度,lng经度
        /// </summary>
        public string Location = "";

        /// <summary>
        /// 检索半径。默认1000米，最大可设置2000米。
        /// </summary>
        public int Radius = 2000;

        /// <summary>
        /// 标签项，与q组合进行检索 日式烧烤/铁板烧、朝外大街
        /// </summary>
        public string Tag;

        /// <summary>
        /// 检索结果详细程度。取值为1 或空，则返回基本信息；取值为2，返回检索POI详细信息
        /// </summary>
        public int Scope = 1;

        /// <summary>
        /// 检索过滤条件，当scope取值为2时，可以设置filter进行排序。
        /// </summary>
        public BaiduPlaceParamFilter Filter;

        /// <summary>
        /// 返回记录数量，默认为10条记录，最大返回结果为20条。
        /// </summary>
        public int PageSize = 10;
        /// <summary>
        /// 分页页码，默认为0,0代表第一页，1代表第二页，以此类推。
        /// </summary>
        public int PageNum = 1;

        public override string ToString()
        {
            if (string.IsNullOrWhiteSpace(Query)
              || (string.IsNullOrWhiteSpace(Region) && string.IsNullOrWhiteSpace(Bounds) && string.IsNullOrWhiteSpace(Location)))
                throw new ArgumentException(BaiduErrorMessages.LackParam);

            string s = "";

            s += "&query=" + System.Web.HttpUtility.UrlEncode(Query);

            if (!string.IsNullOrWhiteSpace(Region))
            {
                s += "&region=" + System.Web.HttpUtility.UrlEncode(Region);
            }
            else
            {
                if (!string.IsNullOrWhiteSpace(Bounds))
                {
                    s += "&bounds=" + Bounds;
                }
                else
                {
                    s += "&location=" + Location + "&radius=" + Radius;
                }
            }
            if (!string.IsNullOrWhiteSpace(Tag))
                s += "&tag=" + System.Web.HttpUtility.UrlEncode(Tag);

            s += "&scope=" + Scope;

            if (Filter != null)
                s += "&filter=" + Filter;

            s += "&page_size=" + PageSize;

            s += "&page_num=" + PageNum;

            return base.ToString() + s;
        }
    }

    public enum IndustryType
    {
        /// <summary>
        /// 宾馆
        /// </summary>
        Hotel,
        /// <summary>
        /// 餐饮
        /// </summary>
        Cater,
        /// <summary>
        /// 生活娱乐
        /// </summary>
        Life
    }

    public enum HotelSortName
    {
        /// <summary>
        /// 默认
        /// </summary>
        Default,
        /// <summary>
        /// 价格
        /// </summary>
        Price,
        /// <summary>
        /// 好评
        /// </summary>
        TotalScore,
        /// <summary>
        /// 星级
        /// </summary>
        Level,
        /// <summary>
        /// 卫生
        /// </summary>
        HealthScore,
        /// <summary>
        /// 距离排序，只有周边检索有效
        /// </summary>
        Distance
    }

    public class BaiduPlaceParamFilter
    {
        /// <summary>
        /// 行业类型
        /// </summary>
        public IndustryType IndustryType;

        /// <summary>
        /// 排序字段
        /// </summary>
        public dynamic SortName = HotelSortName.Default;

        /// <summary>
        /// 排序规则，取值如下：0：从高到低，1：从低到高；
        /// </summary>
        public int SortRule;

        /// <summary>
        /// 价格区间 100,200
        /// </summary>
        public string PriceSection = "";

        /// <summary>
        /// 是否有团购，1为有团购，0为无团购；
        /// </summary>
        public bool Groupon;

        /// <summary>
        /// 是否打折，1为有打折，0为无打折；
        /// </summary>
        public bool Discount;

        public override string ToString()
        {
            const string s = "industry_type:{0}|sort_name:{1}|";
            const string s1 = "sort_rule:{0}|price_section:{1}|groupon:{2}|discount:{3}";
            string result = "";

            switch (IndustryType)
            {
                case IndustryType.Cater:
                    CaterSortName caterSortName = SortName;
                    switch (caterSortName)
                    {
                        case CaterSortName.Default:
                            result = string.Format(s, "cater", "default");
                            break;
                        case CaterSortName.Distance:
                            result = string.Format(s, "cater", "distance");
                            break;
                        case CaterSortName.OverallRating:
                            result = string.Format(s, "cater", "overall_rating");
                            break;
                        case CaterSortName.Price:
                            result = string.Format(s, "cater", "price");
                            break;
                        case CaterSortName.ServiceRating:
                            result = string.Format(s, "cater", "service_rating");
                            break;
                        case CaterSortName.TasteRating:
                            return string.Format(s, "cater", "taste_rating");
                    }
                    break;
                case IndustryType.Hotel:
                    HotelSortName hotelSortName = SortName;
                    switch (hotelSortName)
                    {
                        case HotelSortName.Default:
                            result = string.Format(s, "hotel", "default");
                            break;
                        case HotelSortName.Distance:
                            result = string.Format(s, "hotel", "distance");
                            break;
                        case HotelSortName.Price:
                            result = string.Format(s, "hotel", "price");
                            break;
                        case HotelSortName.HealthScore:
                            result = string.Format(s, "hotel", "health_score");
                            break;
                        case HotelSortName.Level:
                            result = string.Format(s, "hotel", "level");
                            break;
                        case HotelSortName.TotalScore:
                            result = string.Format(s, "hotel", "total_score");
                            break;
                    }
                    break;
                case IndustryType.Life:
                    LifeSortName lifeSortName = SortName;
                    switch (lifeSortName)
                    {
                        case LifeSortName.Default:
                            result = string.Format(s, "hotel", "default");
                            break;
                        case LifeSortName.Distance:
                            result = string.Format(s, "hotel", "distance");
                            break;
                        case LifeSortName.Price:
                            result = string.Format(s, "hotel", "price");
                            break;
                        case LifeSortName.CommentNum:
                            result = string.Format(s, "hotel", "comment_num");
                            break;
                        case LifeSortName.OverallRating:
                            result = string.Format(s, "hotel", "overall_rating");
                            break;
                    }
                    break;
            }
            return result + string.Format(s1, SortRule, PriceSection, Groupon ? 1 : 0, Discount ? 1 : 0);
        }
    }
}
